package com.ruoyi.web.controller.app;

import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.impl.AppAuthServiceImpl;
import com.ruoyi.system.service.impl.SmsServiceImpl;

/**
 * APP认证控制器
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/app/auth")
public class AppAuthController extends BaseController
{
    @Autowired
    private AppAuthServiceImpl appAuthService;

    @Autowired
    private SmsServiceImpl smsService;

    /**
     * 发送短信验证码
     */
    @Anonymous
    @PostMapping("/sendSms")
    public AjaxResult sendSms(@RequestBody Map<String, String> params)
    {
        String phone = params.get("phone");
        String type = params.get("type");

        if (StringUtils.isEmpty(phone))
        {
            return error("手机号不能为空");
        }

        if (StringUtils.isEmpty(type))
        {
            return error("短信类型不能为空");
        }

        // 简单的手机号格式验证
        if (!phone.matches("^1[3-9]\\d{9}$"))
        {
            return error("手机号格式不正确");
        }

        try
        {
            smsService.sendSmsCode(phone, type);
            return success("发送成功");
        }
        catch (Exception e)
        {
            return error(e.getMessage());
        }
    }

    /**
     * 用户注册
     */
    @Anonymous
    @PostMapping("/register")
    public AjaxResult register(@RequestBody Map<String, String> params)
    {
        String phone = params.get("phone");
        String smsCode = params.get("smsCode");
        String password = params.get("password");
        String nickname = params.get("nickname");

        if (StringUtils.isEmpty(phone))
        {
            return error("手机号不能为空");
        }

        if (StringUtils.isEmpty(smsCode))
        {
            return error("验证码不能为空");
        }

        try
        {
            Map<String, Object> result = appAuthService.register(phone, smsCode, password, nickname);
            return AjaxResult.success("注册成功", result);
        }
        catch (Exception e)
        {
            return error(e.getMessage());
        }
    }

    /**
     * 用户登录
     */
    @Anonymous
    @PostMapping("/login")
    public AjaxResult login(@RequestBody Map<String, String> params)
    {
        String phone = params.get("phone");
        String password = params.get("password");
        String smsCode = params.get("smsCode");
        String loginType = params.get("loginType");
        String nickname = params.get("nickname");

        if (StringUtils.isEmpty(phone))
        {
            return error("手机号不能为空");
        }

        if (StringUtils.isEmpty(loginType))
        {
            return error("登录类型不能为空");
        }

        // 密码登录验证
        if ("1".equals(loginType) && StringUtils.isEmpty(password))
        {
            return error("密码不能为空");
        }

        // 短信登录验证
        if ("2".equals(loginType) && StringUtils.isEmpty(smsCode))
        {
            return error("验证码不能为空");
        }

        try
        {
            Map<String, Object> result = appAuthService.login(phone, password, smsCode, loginType, nickname);
            return AjaxResult.success("登录成功", result);
        }
        catch (Exception e)
        {
            return error(e.getMessage());
        }
    }

    /**
     * 找回密码
     */
    @Anonymous
    @PostMapping("/forgot")
    public AjaxResult forgotPassword(@RequestBody Map<String, String> params)
    {
        String phone = params.get("phone");
        String smsCode = params.get("smsCode");
        String newPassword = params.get("newPassword");

        if (StringUtils.isEmpty(phone))
        {
            return error("手机号不能为空");
        }

        if (StringUtils.isEmpty(smsCode))
        {
            return error("验证码不能为空");
        }

        if (StringUtils.isEmpty(newPassword))
        {
            return error("新密码不能为空");
        }

        try
        {
            appAuthService.forgotPassword(phone, smsCode, newPassword);
            return success("密码重置成功");
        }
        catch (Exception e)
        {
            return error(e.getMessage());
        }
    }

    /**
     * 退出登录
     */
    @Anonymous
    @PostMapping("/logout")
    public AjaxResult logout(@RequestHeader(value = "App-Token", required = false) String token)
    {
        try
        {
            appAuthService.logout(token);
            return success("退出成功");
        }
        catch (Exception e)
        {
            return error(e.getMessage());
        }
    }

    /**
     * 获取用户信息
     */
    @Anonymous
    @PostMapping("/getUserInfo")
    public AjaxResult getUserInfo(@RequestHeader(value = "App-Token", required = false) String token)
    {
        if (StringUtils.isEmpty(token))
        {
            return error("Token不能为空");
        }

        try
        {
            Map<String, Object> userInfo = appAuthService.getUserInfo(token);
            if (userInfo == null)
            {
                return error("Token已过期，请重新登录");
            }
            return AjaxResult.success("获取成功", userInfo);
        }
        catch (Exception e)
        {
            return error(e.getMessage());
        }
    }
}
